<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="商品标识" prop="goodId">
        <el-input v-model="dataForm.goodId" placeholder="商品标识"></el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="goodName">
        <el-input v-model="dataForm.goodName" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品编码" prop="goodCode">
        <el-input v-model="dataForm.goodCode" placeholder="商品编码"></el-input>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderSn">
        <el-input v-model="dataForm.orderSn" placeholder="订单编号"></el-input>
      </el-form-item>
      <el-form-item label="订单金额" prop="orderMoney">
        <el-input v-model="dataForm.orderMoney" placeholder="订单金额"></el-input>
      </el-form-item>
      <el-form-item label="订单时间" prop="orderTime">
        <el-input v-model="dataForm.orderTime" placeholder="订单时间"></el-input>
      </el-form-item>
      <el-form-item label="会员标识" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="会员标识"></el-input>
      </el-form-item>
      <el-form-item label="会员编号" prop="userCode">
        <el-input v-model="dataForm.userCode" placeholder="会员编号"></el-input>
      </el-form-item>
      <el-form-item label="评论内容" prop="commentContent">
        <el-input v-model="dataForm.commentContent" placeholder="评论内容"></el-input>
      </el-form-item>
      <el-form-item label="评论时间" prop="createUtc">
        <el-input v-model="dataForm.createUtc" placeholder="评论时间"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="modifyUtc">
        <el-input v-model="dataForm.modifyUtc" placeholder="更新时间"></el-input>
      </el-form-item>
      <el-form-item label="评论类型" prop="commentType">
        <el-input v-model="dataForm.commentType" placeholder="评论类型"></el-input>
      </el-form-item>
      <el-form-item label="是否删除(数字0：未插入，1.未删除,2：删除，)" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否删除(数字0：未插入，1.未删除,2：删除，)"></el-input>
      </el-form-item>
      <el-form-item label="版本号(数字0：未插入)" prop="version">
        <el-input v-model="dataForm.version" placeholder="版本号(数字0：未插入)"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          goodId: '',
          goodName: '',
          goodCode: '',
          orderSn: '',
          orderMoney: '',
          orderTime: '',
          userId: '',
          userCode: '',
          commentContent: '',
          createUtc: '',
          modifyUtc: '',
          commentType: '',
          isDeleted: '',
          version: ''
        },
        dataRule: {
          goodId: [
            { required: true, message: '商品标识不能为空', trigger: 'blur' }
          ],
          goodName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodCode: [
            { required: true, message: '商品编码不能为空', trigger: 'blur' }
          ],
          orderSn: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          orderMoney: [
            { required: true, message: '订单金额不能为空', trigger: 'blur' }
          ],
          orderTime: [
            { required: true, message: '订单时间不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '会员标识不能为空', trigger: 'blur' }
          ],
          userCode: [
            { required: true, message: '会员编号不能为空', trigger: 'blur' }
          ],
          commentContent: [
            { required: true, message: '评论内容不能为空', trigger: 'blur' }
          ],
          createUtc: [
            { required: true, message: '评论时间不能为空', trigger: 'blur' }
          ],
          modifyUtc: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          commentType: [
            { required: true, message: '评论类型不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除(数字0：未插入，1.未删除,2：删除，)不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号(数字0：未插入)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.upkgoodscomment.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodId = data.upkgoodscomment.goodId
                this.dataForm.goodName = data.upkgoodscomment.goodName
                this.dataForm.goodCode = data.upkgoodscomment.goodCode
                this.dataForm.orderSn = data.upkgoodscomment.orderSn
                this.dataForm.orderMoney = data.upkgoodscomment.orderMoney
                this.dataForm.orderTime = data.upkgoodscomment.orderTime
                this.dataForm.userId = data.upkgoodscomment.userId
                this.dataForm.userCode = data.upkgoodscomment.userCode
                this.dataForm.commentContent = data.upkgoodscomment.commentContent
                this.dataForm.createUtc = data.upkgoodscomment.createUtc
                this.dataForm.modifyUtc = data.upkgoodscomment.modifyUtc
                this.dataForm.commentType = data.upkgoodscomment.commentType
                this.dataForm.isDeleted = data.upkgoodscomment.isDeleted
                this.dataForm.version = data.upkgoodscomment.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'goodId': this.dataForm.goodId,
              'goodName': this.dataForm.goodName,
              'goodCode': this.dataForm.goodCode,
              'orderSn': this.dataForm.orderSn,
              'orderMoney': this.dataForm.orderMoney,
              'orderTime': this.dataForm.orderTime,
              'userId': this.dataForm.userId,
              'userCode': this.dataForm.userCode,
              'commentContent': this.dataForm.commentContent,
              'createUtc': this.dataForm.createUtc,
              'modifyUtc': this.dataForm.modifyUtc,
              'commentType': this.dataForm.commentType,
              'isDeleted': this.dataForm.isDeleted,
              'version': this.dataForm.version
            }
            var tick = !this.dataForm.id ? API.upkgoodscomment.add(params) : API.upkgoodscomment.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
